package com.travel.yang.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.yang.bean.RoleWithStatus;
import com.travel.yang.mapper.AdminMapper;
import com.travel.yang.mapper.RoleMapper;
import com.travel.yang.pojo.Admin;
import com.travel.yang.pojo.Permission;
import com.travel.yang.pojo.Role;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;            //注入Mapper
    @Autowired
    private RoleMapper roleMapper;
    // 注入密码加密器（与Spring Security配置的加密器一致）
    @Autowired
    private PasswordEncoder passwordEncoder;

    public Page<Admin> findPage(int page, int size){
        Page selectPage = adminMapper.selectPage(new Page(page, size), null);
        return selectPage;
    }


    //新增管理员
    public void add(Admin admin){
        //对录入数据库的密码进行加密处理
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminMapper.insert(admin);
    }


    //查询用户信息
    public Admin findById(Integer aid){
        return adminMapper.selectById(aid);
    }


    //修改用户信息
    public void update(Admin admin){
        //如果旧密码和新密码一样，则不加密
        String oldPassword = adminMapper.selectById(admin.getAid()).getPassword();
        //新密码
        String newPassword = admin.getPassword();
        if(!oldPassword.equals(newPassword)){
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        }
        adminMapper.updateById(admin);
    }


    //查询用户详情
    public Admin findDesc(Integer aid){
        return adminMapper.findDesc(aid);
    }

    //查询用户的角色情况
    public List<RoleWithStatus> findRole(Integer aid){
        //1、查询所有角色
        List<Role> roles = roleMapper.selectList(null);
        //2、查询用户拥有的角色
        List<Integer> rids = roleMapper.findRoleIdByAdmin(aid);
        //3、构建带有状态的角色集合，拥有状态为true，否则为false
        List<RoleWithStatus> roleList = new ArrayList();
        for (Role role : roles) {
            //创建带有状态的角色
            RoleWithStatus roleWithStatus = new RoleWithStatus();
            //复制对象属性的工具类：BeanUtils
            BeanUtils.copyProperties(role,roleWithStatus);
            if(rids.contains(role.getRid())){
                roleWithStatus.setAdminHas(true);
            }else{
                roleWithStatus.setAdminHas(false);
            }
            roleList.add(roleWithStatus);
        }
        return roleList;
    }

    //修改用户角色
    public void updateRole(Integer aid, Integer[] ids){
        //删除用户所有角色
        adminMapper.deleteAdminAllRoles(aid);
        //重新给用户添加角色
        for(Integer rid : ids){
            adminMapper.addAdminRole(aid,rid);
        }
    }

    //修改用户状态
    public void updateStatus(Integer aid){
        Admin admin = adminMapper.selectById(aid);
        admin.setStatus(!admin.isStatus());
        adminMapper.updateById(admin);
    }

    //根据名字查询管理员
    public Admin findByAdminName(String username){
        QueryWrapper<Admin> wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }
    
    //根据名字查询权限
    public List<Permission> findAllPermission(String username){
        return adminMapper.findAllPermission(username);
    }
}
